package yang.itcode.controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yang.itcode.model.Result;
import yang.itcode.service.InventoryDetailService;

import static yang.itcode.constant.ErrorConstant.INVENTORY_DETAIL_ADD_EXCEPTION_CODE;
import static yang.itcode.constant.ErrorConstant.INVENTORY_DETAIL_ADD_EXCEPTION_MESSAGE;

@RestController
@RequestMapping("/api/inventory-detail")
public class InventoryDetailController {

    @Autowired
    private InventoryDetailService inventoryDetailService;

    @PostMapping()
    public Result<Boolean> createInventoryDetail(@RequestBody JSONObject obj) {
        if (!inventoryDetailService.createInventoryDetail(obj)) {
            return Result.error(INVENTORY_DETAIL_ADD_EXCEPTION_CODE, INVENTORY_DETAIL_ADD_EXCEPTION_MESSAGE);
        }
        return Result.success(true);
    }

    @PostMapping("/list")
    public Result<PageInfo<JSONObject>> getInventoryDetailList(@RequestBody JSONObject obj) {
        Integer pageNum = obj.getInt("pageNum");
        Integer pageSize = obj.getInt("pageSize");
        JSONObject condition = obj.getJSONObject("condition");
        return Result.success(inventoryDetailService.getInventoryDetailList(pageNum, pageSize, condition));
    }

    @PostMapping("/change/list")
    public Result<JSONObject> getInventoryDetailChangeList(@RequestBody JSONObject obj) {
        return Result.success(inventoryDetailService.getInventoryDetailChangeList(obj));
    }
}
